//|||||||||||||||||||||||||||||||||||||||||||||||

#include <sstream>
#include "GameProgressMenuState.hpp"
#include "GameApp.hpp"
#include "GameConstants.h"

//|||||||||||||||||||||||||||||||||||||||||||||||

using namespace Ogre;

//|||||||||||||||||||||||||||||||||||||||||||||||

GameProgressMenuState::GameProgressMenuState()
{
	m_FrameEvent = Ogre::FrameEvent();
	m_progress = new GameProgress();
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void GameProgressMenuState::enter()
{
    OgreFramework::getSingletonPtr()->m_pLog->logMessage("Entering NewGameMenuState...");

    m_pSceneMgr = OgreFramework::getSingletonPtr()->m_pRoot->createSceneManager(ST_GENERIC, "NewGameMenuSceneMgr");
    m_pSceneMgr->setAmbientLight(Ogre::ColourValue(0.7f, 0.7f, 0.7f));

    m_pCamera = m_pSceneMgr->createCamera("NewGameMenuCam");
    m_pCamera->setPosition(Vector3(0, 25, -50));
    m_pCamera->lookAt(Vector3(0, 0, 0));
    m_pCamera->setNearClipDistance(1);

    m_pCamera->setAspectRatio(Real(OgreFramework::getSingletonPtr()->m_pViewport->getActualWidth()) /
        Real(OgreFramework::getSingletonPtr()->m_pViewport->getActualHeight()));

    OgreFramework::getSingletonPtr()->m_pViewport->setCamera(m_pCamera);

    OgreFramework::getSingletonPtr()->m_pTrayMgr->destroyAllWidgets();
//    OgreFramework::getSingletonPtr()->m_pTrayMgr->showFrameStats(OgreBites::TL_BOTTOMLEFT);
    OgreFramework::getSingletonPtr()->m_pTrayMgr->showLogo(OgreBites::TL_BOTTOMRIGHT);
    OgreFramework::getSingletonPtr()->m_pTrayMgr->showCursor();
	if(m_progress == NULL)
	{
		m_progress = new GameProgress();
	}
	
	buildGUI();

    createScene();
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void GameProgressMenuState::buildGUI()
{
	// top buttons
	OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_TOP, "SaveBtn", "Save", 250);
	OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_TOP, "TipBtn", "View Earned Tips", 250);
    OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_TOP, "ExitBtn", "Quit", 250);

	// game 1 buttons
	OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel(OgreBites::TL_LEFT, "Game1", "First Game", 350);
	OgreFramework::getSingletonPtr()->m_pTrayMgr->createSeparator(OgreBites::TL_LEFT, "Game1Separator");

	OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_LEFT, "Game1Lev1Easy", "Level 1 - Easy", 250);
	int NumberOfLevels = 1;
	for(int i = 1; i < NbGame1Easy; i++) {
		NumberOfLevels++;
		std::ostringstream  name;
		name << "Game1Lev" << NumberOfLevels << "Easy";
		std::ostringstream  label;
		label << "Level " << NumberOfLevels << " - Easy";
		if(m_progress->game1Easy[i-1] == Achievement::NotDone) {
			OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel
				(OgreBites::TL_LEFT, name.str(), label.str(), 250);
		} else {
			OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton
				(OgreBites::TL_LEFT, name.str(), label.str(), 250);
		}
	}

	// game 1 medium first level
	NumberOfLevels++;
	std::ostringstream  name2;
	name2 << "Game1Lev" << NumberOfLevels << "Medium";
	std::ostringstream  label2;
	label2 << "Level " << NumberOfLevels << " - Medium";
	if(m_progress->game1Easy[NbGame1Easy-1] == Achievement::NotDone) {
		OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel
			(OgreBites::TL_LEFT, name2.str(), label2.str(), 250);
	} else {
		OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton
			(OgreBites::TL_LEFT, name2.str(), label2.str(), 250);
	}
	
	// game 1 medium other levels
	for(int i = 1; i < NbGame1Medium; i++) {
		NumberOfLevels++;
		std::ostringstream  name;
		name << "Game1Lev" << NumberOfLevels << "Medium";
		std::ostringstream  label;
		label << "Level " << NumberOfLevels << " - Medium";
		if(m_progress->game1Medium[i-1] == Achievement::NotDone) {
			OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel
				(OgreBites::TL_LEFT, name.str(), label.str(), 250);
		} else {
			OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton
				(OgreBites::TL_LEFT, name.str(), label.str(), 250);
		}
	}
	
	// game 1 hard first level
	NumberOfLevels++;
	std::ostringstream  name3;
	name3 << "Game1Lev" << NumberOfLevels << "Hard";
	std::ostringstream  label3;
	label3 << "Level " << NumberOfLevels << " - Hard";
	if(m_progress->game1Medium[NbGame1Medium-1] == Achievement::NotDone) {
		OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel
			(OgreBites::TL_LEFT, name3.str(), label3.str(), 250);
	} else {
		OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel
			(OgreBites::TL_LEFT, name3.str(), label3.str(), 250);
	}

	// game 1 hard other levels
	for(int i = 1; i < NbGame1Hard; i++) {
		NumberOfLevels++;
		std::ostringstream  name;
		name << "Game1Lev" << NumberOfLevels << "Hard";
		std::ostringstream  label;
		label << "Level " << NumberOfLevels << " - Hard";
		if(m_progress->game1Hard[i-1] == Achievement::NotDone) {
			OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel
				(OgreBites::TL_LEFT, name.str(), label.str(), 250);
		} else {
			OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton
				(OgreBites::TL_LEFT, name.str(), label.str(), 250);
		}
	}


	// game 2 buttons
	OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel(OgreBites::TL_RIGHT, "Game2", "Second Game", 350);
	OgreFramework::getSingletonPtr()->m_pTrayMgr->createSeparator(OgreBites::TL_RIGHT, "Game2Separator");

	OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_RIGHT, "Game2Lev1Easy", " Level 1 - Easy", 250);
	
	NumberOfLevels = 1;
	for(int i = 1; i < NbGame2Easy; i++) {
		NumberOfLevels++;
		std::ostringstream  name;
		name << "Game2Lev" << NumberOfLevels << "Easy";
		std::ostringstream  label;
		label << "Level " << NumberOfLevels << " - Easy";
		if(m_progress->game2Easy[i-1] == Achievement::NotDone) {
			OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel
				(OgreBites::TL_RIGHT, name.str(), label.str(), 250);
		} else {
			OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton
				(OgreBites::TL_RIGHT, name.str(), label.str(), 250);
		}
	}

	// game 2 medium first level
	NumberOfLevels++;
	std::ostringstream  name4;
	name4 << "Game2Lev" << NumberOfLevels << "Medium";
	std::ostringstream  label4;
	label4 << "Level " << NumberOfLevels << " - Medium";
	if(m_progress->game2Easy[NbGame2Easy-1] == Achievement::NotDone) {
		OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel
			(OgreBites::TL_RIGHT, name4.str(), label4.str(), 250);
	} else {
		OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton
			(OgreBites::TL_RIGHT, name4.str(), label4.str(), 250);
	}
	
	// game 2 medium other levels
	for(int i = 1; i < NbGame2Medium; i++) {
		NumberOfLevels++;
		std::ostringstream  name;
		name << "Game2Lev" << NumberOfLevels << "Medium";
		std::ostringstream  label;
		label << "Level " << NumberOfLevels << " - Medium";
		if(m_progress->game2Medium[i-1] == Achievement::NotDone) {
			OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel
				(OgreBites::TL_RIGHT, name.str(), label.str(), 250);
		} else {
			OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton
				(OgreBites::TL_RIGHT, name.str(), label.str(), 250);
		}
	}
	
	// game 2 hard first level
	NumberOfLevels++;
	std::ostringstream  name5;
	name5 << "Game2Lev" << NumberOfLevels << "Hard";
	std::ostringstream  label5;
	label5 << "Level " << NumberOfLevels << " - Hard";
	if(m_progress->game2Medium[NbGame2Medium-1] == Achievement::NotDone) {
		OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel
			(OgreBites::TL_RIGHT, name5.str(), label5.str(), 250);
	} else {
		OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel
			(OgreBites::TL_RIGHT, name5.str(), label5.str(), 250);
	}

	// game 1 hard other levels
	for(int i = 1; i < NbGame2Hard; i++) {
		NumberOfLevels++;
		std::ostringstream  name;
		name << "Game2Lev" << NumberOfLevels << "Hard";
		std::ostringstream  label;
		label << "Level " << NumberOfLevels << " - Hard";
		if(m_progress->game2Hard[i-1] == Achievement::NotDone) {
			OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel
				(OgreBites::TL_RIGHT, name.str(), label.str(), 250);
		} else {
			OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton
				(OgreBites::TL_RIGHT, name.str(), label.str(), 250);
		}
	}


	//OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_CENTER, "PlayGame1", "Play Game 1", 250);
    //OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_CENTER, "PlayGame2", "Play Game 2", 250);
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void GameProgressMenuState::createScene()
{
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void GameProgressMenuState::exit()
{
    OgreFramework::getSingletonPtr()->m_pLog->logMessage("Leaving GameProgressMenuState...");

    m_pSceneMgr->destroyCamera(m_pCamera);
    if(m_pSceneMgr)
        OgreFramework::getSingletonPtr()->m_pRoot->destroySceneManager(m_pSceneMgr);

    OgreFramework::getSingletonPtr()->m_pTrayMgr->clearAllTrays();
    OgreFramework::getSingletonPtr()->m_pTrayMgr->destroyAllWidgets();
    OgreFramework::getSingletonPtr()->m_pTrayMgr->setListener(0);
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool GameProgressMenuState::keyPressed(const OIS::KeyEvent &keyEventRef)
{
    if(OgreFramework::getSingletonPtr()->m_pKeyboard->isKeyDown(OIS::KC_ESCAPE))
    {
        m_bQuit = true;
        return true;
    }

    OgreFramework::getSingletonPtr()->keyPressed(keyEventRef);
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool GameProgressMenuState::keyReleased(const OIS::KeyEvent &keyEventRef)
{
    OgreFramework::getSingletonPtr()->keyReleased(keyEventRef);
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool GameProgressMenuState::mouseMoved(const OIS::MouseEvent &evt)
{
    if(OgreFramework::getSingletonPtr()->m_pTrayMgr->injectMouseMove(evt)) return true;
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool GameProgressMenuState::mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
    if(OgreFramework::getSingletonPtr()->m_pTrayMgr->injectMouseDown(evt, id)) return true;
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool GameProgressMenuState::mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
    if(OgreFramework::getSingletonPtr()->m_pTrayMgr->injectMouseUp(evt, id)) return true;
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void GameProgressMenuState::update(double timeSinceLastFrame)
{
    m_FrameEvent.timeSinceLastFrame = timeSinceLastFrame;
    OgreFramework::getSingletonPtr()->m_pTrayMgr->frameRenderingQueued(m_FrameEvent);

    if(m_bQuit == true)
    {
        shutdown();
        return;
    }
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void GameProgressMenuState::buttonHit(OgreBites::Button *button)
{
	

	if(button->getName() == "ExitBtn") {
        m_bQuit = true;
	} else if(button->getName() == "SaveBtn") {
		m_app->saveGame(m_progress);
	} else if(button->getName() == "Game1Lev1Easy") {
		changeAppState(findByName("Game1State"));
	} else if(button->getName() == "Game2Lev1Easy") {
		changeAppState(findByName("Game2State"));
	} else if(button->getName() == "TipBtn") {
		changeAppState(findByName("TipsMenuState"));
	}
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void GameProgressMenuState::setProgress(GameProgress* progress)
{
	m_progress = progress;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

GameProgress* GameProgressMenuState::getProgress()
{
	return m_progress;
}


//|||||||||||||||||||||||||||||||||||||||||||||||

void GameProgressMenuState::setGameApp(GameApp* app)
{
	m_app = app;
}